define(['egis'], function (egis) {
    Window.deserializer = Window.deserializer || new egis.core.Deserializer();
    Window.container = Window.container || {};
    dsBridge.register('create_SimpleConsultService', function (sourceId, clinetId, plotLayerId, plotPlugInId, srid, projectId, wsUrl, url, name, authType, clientId, clientSecret,tokenUrl) {
        try {
            var plotLayer = Window.container[plotLayerId];
            var project = Window.container[projectId];
            //var restHttp = Window.container[restHttpId];
            var plotPlugIn = Window.container[plotPlugInId];
            var wCPSCallback = new egis.ews.WCPSCallback({
                plotLayer: plotLayer,
                //http: restHttp,
                srid: srid,
                projectService: project,
                deserializer: Window.deserializer
            });

            var simpleConsultService = new egis.ews.SimpleConsultService({
                wsUrl: wsUrl,
                cid: clinetId,
                url: url,
                //http: restHttp,
                callback: wCPSCallback,
                name: name,
                deserializer: Window.deserializer,
                authType: authType,
                clientId: clientId,
                clientSecret: clientSecret,
                tokenUrl:tokenUrl
            });
            plotPlugIn.addSucessor(simpleConsultService);
            var oldAddMessage = simpleConsultService.add;
            var oldRemoveMessage = simpleConsultService.remove;
            simpleConsultService.add = function (message, callBack) {
                if (!!callBack) {
                    oldAddMessage.call(this, message);
                } else {
                    oldAddMessage.call(this, message);
                    dsBridge.call("egis.SimpleConsultService_onAddMessage", [sourceId, this.cid, JSON.stringify(message)]);
                }
            }
            simpleConsultService.remove = function (messageId, callBack) {
                if (!!callBack) {
                    oldRemoveMessage.call(this, message);
                } else {
                    oldRemoveMessage.call(this, messageId);
                    dsBridge.call("egis.SimpleConsultService_onRemoveMessage", [sourceId, this.cid, messageId]);
                }
            }
            simpleConsultService.on('ONMEMBERJOINED', function (member) {
                //dsBridge.call("egis.Map_resolutionChangedEvent", [JSON.stringify(member)]);
                dsBridge.call("egis.SimpleConsultService_OnMemberJoined", [sourceId, JSON.stringify(member)]);
            })
            simpleConsultService.on('ONMEMBERLEAVED', function (memberId) {
                //dsBridge.call("egis.Map_resolutionChangedEvent", [memberId]);
                dsBridge.call("egis.SimpleConsultService_onMemberLeaved", [sourceId, memberId]);
            })
            simpleConsultService.on('ONENDMEETING', function () {
                //dsBridge.call("egis.Map_resolutionChangedEvent", []);
                dsBridge.call("egis.SimpleConsultService_onEndMeeting", [sourceId]);
            })
            simpleConsultService.on('ONADDMESSAGE', function (memberId, message) {
                //dsBridge.call("egis.Map_resolutionChangedEvent", [memberId, JSON.stringify(message)]);
                var msg = undefined;
                if (!!message) {
                    message.unset();
                    msg = JSON.stringify(message);
                }
                dsBridge.call("egis.SimpleConsultService_onAddMessage", [sourceId, memberId, msg]);
            })
            simpleConsultService.on('ONUPDATEMESSAGE', function (memberId, message) {
                //dsBridge.call("egis.Map_resolutionChangedEvent", [memberId, JSON.stringify(message)]);
                dsBridge.call("egis.SimpleConsultService_onUpdateMessage", [sourceId, memberId, JSON.stringify(message)]);
            })
            simpleConsultService.on('ONREMOVEMESSAGE', function (memberId, messageId) {
                //dsBridge.call("egis.Map_resolutionChangedEvent", [memberId, messageId]);
                dsBridge.call("egis.SimpleConsultService_onRemoveMessage", [sourceId, memberId, messageId]);
            })
            simpleConsultService.on('ONCONNECTED', function () {
                //dsBridge.call("egis.Map_resolutionChangedEvent", []);
                dsBridge.call("egis.SimpleConsultService_onConnected", [sourceId]);
            })
            simpleConsultService.on('ONDISCONNECTED', function () {
                //dsBridge.call("egis.Map_resolutionChangedEvent", []);
                dsBridge.call("egis.SimpleConsultService_onDisConnected", [sourceId]);
            })

            Window.container[sourceId] = simpleConsultService;
        }
        catch (e) {
            alert(e)
        }
    })
    dsBridge.registerAsyn('SimpleConsultService_init', function (sourceId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.init().then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })

    dsBridge.registerAsyn('SimpleConsultService_unset', function (sourceId) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.unset();
    })

    dsBridge.registerAsyn('SimpleConsultService_create', function (sourceId, meetingId, layer, name, description, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.create(meetingId, layer, name, description).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_join', function (sourceId, layer, meetingId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.join(layer, meetingId).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_unReadCount', function (sourceId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.unReadCount().then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_queryUnRead', function (sourceId, page, size, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.queryUnRead(page, size).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_leave', function (sourceId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.leave().then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_close', function (sourceId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.close().then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })

    dsBridge.registerAsyn('SimpleConsultService_add', function (sourceId, message) {
        var simpleConsultService = Window.container[sourceId];
        message = Window.deserializer.createJsInstance(JSON.parse(message));
        simpleConsultService.add(message, true);
    })
    dsBridge.registerAsyn('SimpleConsultService_update', function (sourceId, message) {
        var simpleConsultService = Window.container[sourceId];
        message = Window.deserializer.createJsInstance(JSON.parse(message));
        simpleConsultService.update(message);
    })
    dsBridge.registerAsyn('SimpleConsultService_remove', function (sourceId, messageId) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.remove(messageId);
    })
    dsBridge.registerAsyn('SimpleConsultService_exist', function (sourceId, layer, meetingId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.exists(layer, meetingId).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_list', function (sourceId, layer, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.list(layer).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })


     dsBridge.registerAsyn('SimpleConsultService_query', function (sourceId, layer, start, end, page, size, responseCallback) {
            var simpleConsultService = Window.container[sourceId];
            simpleConsultService.query(layer, start, end, page, size).then(function (value) {
                responseCallback(JSON.stringify(value));
            })
        })


    dsBridge.registerAsyn('SimpleConsultService_download', function (sourceId, layer, meetingId, responseCallback) {
            var simpleConsultService = Window.container[sourceId];
            simpleConsultService.download(layer, meetingId).then(function (value) {
                responseCallback(JSON.stringify(value));
            })
        })

    dsBridge.registerAsyn('SimpleConsultService_addElement', function (sourceId, element, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.addElement(element).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })

    dsBridge.registerAsyn('SimpleConsultService_removeElement', function (sourceId, id, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.removeElement(id).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })

    dsBridge.registerAsyn('SimpleConsultService_updateElement', function (sourceId, element, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.updateElement(element).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })

    dsBridge.registerAsyn('SimpleConsultService_showElement', function (sourceId, element, visible, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.showElement(element, visible).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_clear', function (sourceId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.clear().then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_selectElement', function (sourceId, element, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.selectElement(element).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_unSelectElement', function (sourceId, element, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.unSelectElement(element).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })

    dsBridge.registerAsyn('SimpleConsultService_onMemberJoined', function (sourceId, member, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.onMemberJoined(member).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_onMemberLeaved', function (sourceId, memberId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.onMemberLeaved(memberId).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_onEnd', function (sourceId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.onEnd().then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_onAdd', function (sourceId, memberId, message, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.onAdd(memberId, message).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_onUpdate', function (sourceId, memberId, message, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.onUpdate(memberId, message).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_onRemove', function (sourceId, memberId, messageId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.onRemove(memberId, messageId).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_onConnected', function (sourceId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.onConnected().then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_onDisconnected', function (sourceId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.onDisconnected().then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_raise', function (sourceId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.raise().then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_on', function (sourceId, name, action, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.on(name, action).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_un', function (sourceId, name, action, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.un(name, action).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })

    dsBridge.registerAsyn('SimpleConsultService_getCapability', function (sourceId, layer, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.getCapability(layer).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('SimpleConsultService_getCapabilities', function (sourceId, responseCallback) {
        var simpleConsultService = Window.container[sourceId];
        simpleConsultService.getCapabilities().then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
})